package com.hjm.project.system.db.service;

import com.alibaba.fastjson.JSON;
import com.hjm.common.exception.BusinessException;
import com.hjm.common.utils.Aes;
import com.hjm.common.utils.StringUtils;
import com.hjm.project.system.db.domain.Db;
import com.hjm.project.system.db.mapper.DbMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 */
@Service
public class DbServiceImpl implements DbService {
    @Resource
    private DbMapper dbMapper;


    private static final String UPDATE = "2";
    private static final String INSERT = "3";
    private static final String DELETE = "4";
    private static final String OTHER =  "6";


    /**
     *字段名称
     */
    private static final String COLUMN_NAME = "COLUMN_NAME";

    /**
     *字段类型
     */
    private static final String DATA_TYPE = "DATA_TYPE";

    /**
     *字段长度
     */
    private static final String COMMENTS = "COMMENTS";



    /**
     * 获取所有表
     * @Param []
     * @Return java.util.List<java.util.Map < java.lang.String, java.lang.String>>
     */
    @Override
    public List<Map<String, String>> selectTableList(Db db) {
        return dbMapper.selectTableList(db);
    }
    /**
     * 获取一个表
     * @Param []
     * @Return java.util.List<java.util.Map < java.lang.String, java.lang.String>>
     */
    @Override
    public Map<String, Object> selectTableOne(String tableName) {
        return dbMapper.selectTableOne(tableName);
    }

    public List<Map<String,String>> selectTableStructure(Db db) {
        return dbMapper.selectTableStructure(db);
    }

    /**
     * 执行一个语句
     *
     * @Param [content]
     * @Return java.util.List<java.util.Map < java.lang.String, java.lang.String>>
     */
    @Override
    public List<Map<String, String>> perform(String content) {
        return dbMapper.perform(content);
    }

    @Transactional
    @Override
    public int update(String content) {
        return dbMapper.update(content);
    }

    @Transactional
    @Override
    public int del(String content) {
        return dbMapper.del(content);
    }



    @Transactional
    @Override
    public int insert(String content) {
        return dbMapper.insert(content);
    }


    /**
     * 返回自定义查询结果
     * @Param [content]
     * @Return java.lang.String
     */
    @Override
    public String performResult(String content) {
        List<Map<String, String>> perform = dbMapper.perform(content);
        //执行参数
        //结果加密
        String result = Aes.aesEncrypt(JSON.toJSONString(perform));
        return result;
    }
    /**
     * 更新
     * @Param [content,
     *       type UPDATE = "2"
     *            INSERT = "3"
     *            DELETE = "4"
     *       ]
     * @Return int
     */
    @Transactional
    @Override
    public int saveOrUpdate(String content,String type) {
        int result = 1;
        if (UPDATE.equals(type)) {
            result = dbMapper.update(content);
        } else if (INSERT.equals(type)){
            result = dbMapper.insert(content);
        } else if (DELETE.equals(type)){
            result = dbMapper.del(content);
        } else if (OTHER.equals(type)){
            result = dbMapper.update(content);
        } else {
            throw  new BusinessException("未知选项");
        }
        return result;
    }


    /**
     *执行一个更改语句
     * @Param [content]
     * @Return int
     */
    @Transactional
    @Override
    public int updateColumn(Map param) {
        //alter table tableName rename column oldCName to newCName; -- 修改字段名
        //alter table tableName modify (cloumnName 数据类型); -- 修改数据类型
        //alter table tablename modify (colunmsname varchar2(2000)); --修改字段长度(有值得情况下只能增加)
        //　comment on table T3 is '备注';
        //　comment on column T3.id is '备注';
        //alter table  表名  drop column 列名
        //alter table tablename add (column datatype [default value][null/not null],….);
        String sql = null;
        //更改字段名称
        if (COLUMN_NAME.equals(String.valueOf(param.get("field")))) {
             sql = "alter table " + param.get("tableName") + " rename column " + param.get("oldValue") + " to " + param.get("value");
        } else if (DATA_TYPE.equals(String.valueOf(param.get("field")))){
             sql = "alter table " + param.get("tableName") + " modify( " + param.get("column") + " " + param.get("value")  + ") ";
        } else if (COMMENTS.equals(String.valueOf(param.get("field")))){
             sql = "comment on column " + param.get("tableName") + "." + param.get("column") + " is '" + param.get("value") + "'";
        } else if (param.get("value").equals("false") && param.get("oldValue").equals("false") && param.get("field").equals("false")){
            sql =  "alter table " + param.get("tableName") + " drop column " + param.get("column");
        }
        if (sql == null) {
            throw  new BusinessException("请选择更改类型");
        }
        dbMapper.updateColumn(sql);
        return 1;
    }
    @Transactional
    @Override
    public int addColumn(Map param) {
        //        //ALTER TABLE table_name ADD column_2 VARCHAR2(44) DEFAULT '';
        StringBuffer sql = new StringBuffer("alter table ");
        sql.append(param.get("tableName")).append(" add (");
        sql.append(param.get("name")).append(" ");
        if (param.get("type").equals("DATE")) {
            sql.append(param.get("type")).append(" ");
        } else {
            sql.append(param.get("type")).append("(" +param.get("length")+") ");
        }

        //默认值
        if (StringUtils.isNotEmpty(String.valueOf(param.get("default")))) {
            sql.append("default").append(" '" + param.get("default") + "'");
        } else if (param.get("type").equals("DATE")){
            sql.append("default").append(" sysdate ");

        }
        //字段是否可为空
        if (param.get("isNull") == null || param.get("isNull").equals("1")){
            sql.append(" null ");
        } else {
            sql.append(" not null");
        }
        sql.append(" ) ");
        dbMapper.updateColumn(sql.toString());

        //添加备注
        if (StringUtils.isNotEmpty(String.valueOf(param.get("remark")))) {
            String sqls = "comment on column " + param.get("tableName")  + "." +param.get("name") + " is " + "'" + param.get("remark") +"'";
            dbMapper.updateColumn(sqls);
        }
        return 1;
    }







}
